package com.jyk.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyk.blog.po.Blog;
import com.jyk.blog.po.Tag;
import com.jyk.blog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    UserService userService;
    @Autowired
    BlogService blogService;
    @Autowired
    TypeService typeService;
    @Autowired
    TagService tagService;
    @Autowired
    BlogTagService blogTagService;
    @Autowired
    CommentService commentService;

    @RequestMapping("/index")
    public String adminIndex(Model model, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo){
        // 分类和标签信息
        model.addAttribute("typeList", typeService.queryAll());
        model.addAttribute("tagList", tagService.queryAll());
        model.addAttribute("userService", userService);

        //博客信息列表
        Integer pageSize = 4;
        PageHelper.startPage(pageNo, pageSize, "nid desc");
        List<Blog> blogList = blogService.queryAll();
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        model.addAttribute("blogList", pageInfo);

        return "admin/admin_main";
    }

    @RequestMapping("/blogDetail")
    public String blogDetail(Integer id, Model model) {
        // 分类和标签信息
        model.addAttribute("typeList", typeService.queryAll());
        model.addAttribute("tagList", tagService.queryAll());
        model.addAttribute("userService", userService);

        List<Tag> tags = blogTagService.queryByBlog(id);
        String tagInfo = "";
        for (Tag tag : tags) {
            tagInfo = tagInfo + tag.getName() + "  ";
        }
        model.addAttribute("tagInfo", tagInfo);

        Blog blog = blogService.queryById(id);
        model.addAttribute("blog", blog);

        model.addAttribute("contents", blog.getContent().split("\\s+"));

        model.addAttribute("comments", commentService.queryByBlog(id));
        return "admin/admin_blogDetail";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        System.out.println("!!!!!!!!!加验证！！！！！");
        blogService.deleteOne(id);
        return "redirect:index";
    }

    @RequestMapping("/showByType")
    public String showByType(Integer typeID, Model model, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
        // 分类和标签信息
        model.addAttribute("typeList", typeService.queryAll());
        model.addAttribute("tagList", tagService.queryAll());
        model.addAttribute("userService", userService);

        //博客信息列表
        Integer pageSize = 4;
        PageHelper.startPage(pageNo, pageSize, "nid desc");
        List<Blog> blogList = blogService.queryByType(typeID);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        model.addAttribute("blogList", pageInfo);

        model.addAttribute("type", typeService.queryById(typeID));
        return "admin/admin_showByType";
    }

    @RequestMapping("/showByTag")
    public String showByTag(Integer tagID, Model model, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
        // 分类和标签信息
        model.addAttribute("typeList", typeService.queryAll());
        model.addAttribute("tagList", tagService.queryAll());
        model.addAttribute("userService", userService);

        //博客信息列表
        Integer pageSize = 4;
        PageHelper.startPage(pageNo, pageSize, "nid desc");
        List<Blog> blogList = blogTagService.queryByTag(tagID);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        model.addAttribute("blogList", pageInfo);

        model.addAttribute("tag", tagService.queryById(tagID));
        return "admin/admin_showByTag";
    }

    @RequestMapping("/tagEdit")
    public String tagEdit(Model model) {
        // 分类和标签信息
        model.addAttribute("typeList", typeService.queryAll());
        model.addAttribute("tagList", tagService.queryAll());
        model.addAttribute("userService", userService);

        //博客信息列表
        model.addAttribute("tagList", tagService.queryAll());

        return "admin/admin_tagEdit";
    }

    @RequestMapping("/fixTag")
    public String fixTag(Model model, Integer tagID){
        // 分类和标签信息
        model.addAttribute("typeList", typeService.queryAll());
        model.addAttribute("tagList", tagService.queryAll());
        model.addAttribute("userService", userService);
        model.addAttribute("tag", tagService.queryById(tagID));
        return "admin/admin_fixTag";
    }

    @RequestMapping("/fixTagC")
    public String fixTagC(String tagName, Integer id){
        tagService.updateOne(id, tagName);
        return "redirect:tagEdit";
    }

    @RequestMapping("/deleteTag")
    public String deleteTag(Integer tagID){
        System.out.println("!!!!!!!!!加验证！！！！！");
        tagService.deleteOne(tagService.queryById(tagID));
        return "redirect:tagEdit";
    }

    @RequestMapping("/addTag")
    public String addTag(){
        return "admin/admin_tagAdd";
    }

    @RequestMapping("/addTagC")
    public String addTagC(String tagName){
        tagService.insertOne(tagName);
        return "redirect:tagEdit";
    }
}